var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var routes = require('./routes/index');
var login = require('./routes/login');
var app = express();
var session = require('express-session');
var qiniu = require('qiniu');
var list = require('./routes/list');
var detail = require('./routes/detail');
var comment = require('./routes/comment');
//var register = require('./routes/register');
// view engine setup
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
// uncomment after placing your favicon in /public
//app.use(favicon(__dirname + '/public/favicon.ico'));
app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(express.static(path.join(__dirname, 'public')));
app.use(session({
    secret:'keyboard cat',
    resave:false,
    saveUninitialized:true,
    cookie: { maxAge: 60000 }
}));
//app.use(initQiniu());
initQiniu();
app.use('/login',login);
app.use('/list',list);
app.use('/detail',detail);
app.use('/comment', comment);
//app.use('/register', register);
app.use('/', routes);
app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
});
if (app.get('env') === 'development') {
    app.use(function(err, req, res, next) {
        res.status(err.status || 500);
        res.render('error', {
            message: err.message,
            error: err
        });
    });
}


app.use(function(err, req, res, next) {
    res.status(err.status || 500);
    res.render('error', {
        message: err.message,
        error: {}
    });
});

function initQiniu() {
    qiniu.conf.ACCESS_KEY = 'KRDBcezpvvq_xMTVgZ5E91Z5nBxxjBsUt5NQsV6J';
    qiniu.conf.SECRET_KEY = '3SeUk7rgnmh2IAw2Oth1ggvdQAmMC_hpvrKiDj-G';
}
module.exports = app;
